package com.wxtt.android.recyclerview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.ViewGroup;

import com.fast.dev.frame.Adapter.recycleview.FastAdapter;
import com.fast.dev.frame.Adapter.recycleview.IItem;
import com.fast.dev.frame.Adapter.recycleview.adapter.HeaderAdapter;
import com.fast.dev.frame.Adapter.recycleview.adapter.ItemAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明：RecyclerView包装
 *        --- 长按操作，点击操作
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/25 11:21
 * <p/>
 * 版本：verson 1.0
 */
public class WrapRecyclerView<Item extends IItem> {

    //代理的RecyclerView
    private RecyclerView recyclerView;
    private List<Item> mDatas;
    private Context mContext;

    private FastAdapter<Item> mFastAdapter;
    private ItemAdapter<Item> mItemAdapter;
    private HeaderAdapter<Item> mHeaderAdapter;

    private LinearLayoutManager mLinearLayoutManager;
    private GridLayoutManager mGridLayoutManager;
    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;

    /**
     * 说明：构造函数
     */
    public WrapRecyclerView(RecyclerView recyclerView){
        this(recyclerView, null);
    }

    /**
     * 说明：构造函数
     */
    public WrapRecyclerView(RecyclerView recyclerView, List<Item> mData){
        if (recyclerView == null){
            throw new RuntimeException("recyclerView is null");
        }
        this.recyclerView = recyclerView;
        this.mContext = recyclerView.getContext();
        init(mData);
    }

    /**
     * 说明：初始化
     */
    private void init(List<Item> mData){
        if (mFastAdapter == null){
            mFastAdapter = new FastAdapter<>();
        }
        if (mItemAdapter == null){
            mItemAdapter = new ItemAdapter<>();
        }
        if (mHeaderAdapter == null){
            mHeaderAdapter = new HeaderAdapter<>();
        }
        setDefaultMananger();
        setAdapter();
        setData(mData);
    }

    /**
     * 说明：开启多选
     */
    public void setMultiSelect(boolean multiSelect){
        mFastAdapter.withMultiSelect(multiSelect);
        mFastAdapter.withMultiSelectOnLongClick(multiSelect);
    }

    /**
     * 说明：设置数据
     * @param mData
     */
    public void setData(List<Item> mData){
        if (mData == null){
            mData = new ArrayList<>();
        }
        this.mDatas = mData;
        mItemAdapter.set(mData);
    }

    public void setDecoration(RecyclerView.ItemDecoration itemDecoration){
        recyclerView.addItemDecoration(itemDecoration);
    }

    /**
     * 说明：增加数据集合
     * @param position 数据位置
     * @param data 数据集合
     */
    public void add(int position,List<Item> data){
        if (data != null && !data.isEmpty()){
            mItemAdapter.add(position,data);
        }
    }

    /**
     * 说明：设置数据，覆盖
     * @param data
     */
    public void set(List<Item> data){
        if (data != null && !data.isEmpty()){
            mItemAdapter.set(data);
        }
    }

    /**
     * 说明：设置数据，覆盖
     * @param position
     * @param data
     */
    public void set(int position,Item data){
        if (data != null){
            mItemAdapter.set(position,data);
        }
    }

    /**
     * 说明：增加数据集合（默认增加到顶部）
     * @param data 数据集合
     */
    public void add(List<Item> data){
        add(0,data);
    }

    /**
     * 说明：删除一条数据
     * @param position
     */
    public void remove(int position){
        mItemAdapter.remove(position);
    }

    /**
     * 说明：删除范围数据
     * @param position 其实索引
     * @param count 数目
     */
    public void remove(int position,int count){
        mItemAdapter.removeRange(position,count);
    }

    /**
     * 说明：设置Item点击事件
     * @param listener
     */
    public void setOnClick(FastAdapter.OnClickListener<Item> listener){
        if (listener != null){
            mFastAdapter.withOnClickListener(listener);
        }
    }

    /**
     * 说明：设置Item长按点击事件
     * @param listener
     */
    public void setOnLongClick(FastAdapter.OnLongClickListener<Item> listener){
        if (listener != null){
            mFastAdapter.withOnLongClickListener(listener);
        }
    }

    /**
     * 说明：设置Item触摸事件
     * @param listener
     */
    public void setOnTouch(FastAdapter.OnTouchListener<Item> listener){
        if (listener != null){
            mFastAdapter.withOnTouchListener(listener);
        }
    }

    /**
     * 说明：刷新数据
     * @param data
     */
    public void refresh(List<Item> data){
        if (data == null){
            mDatas = new ArrayList<>();
        }else {
            mDatas = data;
        }
    }

    /**
     * 说明：设置Adapter
     */
    public void setAdapter(){
        recyclerView.setAdapter(mItemAdapter.wrap(mHeaderAdapter.wrap(mFastAdapter)));
    }

    public ItemAdapter<Item> getItemAdapter(){
        return mItemAdapter;
    }

    public HeaderAdapter<Item> getHeaderAdapter(){
        return mHeaderAdapter;
    }

    public FastAdapter<Item> getFastAdapter(){
        return mFastAdapter;
    }

    /**
     * 说明：设置界面布局
     */
    public void setDefaultMananger(){
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        setLayoutManager(manager);
    }

    /**
     * 说明：设置界面布局（LayoutManager）
     * @param manager
     */
    public void setLayoutManager(LinearLayoutManager manager){
        this.mLinearLayoutManager = manager;
        this.recyclerView.setLayoutManager(manager);
    }

    /**
     * 说明：设置界面布局（GridLayoutManager）
     * @param manager
     */
    public void setLayoutManager(GridLayoutManager manager){
        this.mGridLayoutManager = manager;
        this.recyclerView.setLayoutManager(manager);
    }

    /**
     * 说明：设置界面布局（StaggeredGridLayoutManager）
     * @param manager
     */
    public void setLayoutManager(StaggeredGridLayoutManager manager){
        this.mStaggeredGridLayoutManager = manager;
        this.recyclerView.setLayoutManager(manager);
    }

    /**
     * 说明：RecyclerView跳转到顶部
     * @param recyclerView
     * @return
     */
    public static boolean smoonToTop(final RecyclerView recyclerView){
        if (recyclerView != null) {
            int firstChildTop = 0;
            if (recyclerView.getChildCount() > 0) {
                // 如果RecyclerView的子控件数量不为0，获取第一个子控件的top

                // 解决item的topMargin不为0时不能触发下拉刷新
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) recyclerView.getChildAt(0).getLayoutParams();
                firstChildTop = recyclerView.getChildAt(0).getTop() - layoutParams.topMargin - recyclerView.getPaddingTop();
            }

            RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
            if (manager == null) {
                return true;
            }
            if (manager.getItemCount() == 0) {
                return true;
            }

            if (manager instanceof LinearLayoutManager) {
                LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
                if (layoutManager.findFirstCompletelyVisibleItemPosition() < 1 && firstChildTop == 0) {
                    return true;
                }
            } else if (manager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;

                int[] out = layoutManager.findFirstCompletelyVisibleItemPositions(null);
                if (out[0] < 1) {
                    return true;
                }
            }
        }
        return true;
    }

    /**
     * 说明：到顶部
     */
    public void toTop(){
        toPosition(0);
    }

    public void toPosition(int position){
        if (recyclerView != null && recyclerView.getLayoutManager()!=null){
            int count = recyclerView.getLayoutManager().getItemCount();
            if (position >= 0 && position < count){
                recyclerView.getLayoutManager().scrollToPosition(position);
            }
        }
    }
}
